﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;

namespace Pigs.Enemies
{
    class Ninja_Pig : pigs.Pig
    {
        private int time;
        private Vector2 destination;
        private Vector2 start;
        private int numPlayers;

        // Animation variables
        int frameCount = 0; // Current frame
        int frameSkipX = 84; // Width of a frame
        int animationCount; // How many ticks since the last frame change.
        int animationMax; // How many ticks to change frame after. 
        Rectangle source;

        /// <summary>
        /// make the new pig
        /// </summary>
        /// <param name="texture">texture to use</param>
        /// <param name="position">where should the pig start out</param>
        public Ninja_Pig(Texture2D texture, Vector2 position, int numbPlayers)
            : base(texture, position)
        {
            START_HEALTH = 1;
            START_DAMAGE = 10;
            START_SPEED = 3.0f;
            VALUE = 225;

            this.position = position;
            this.numPlayers = numbPlayers;
            this.active = true; // We initialize the enemy to be active so it will be update in the game
            if (numPlayers == 1) { this.health = START_HEALTH; }
            else if (numPlayers > 1 && numPlayers < 3) { this.health = START_HEALTH * 2; }
            else { this.health = this.START_HEALTH * 3; }
            if (numPlayers == 1) { this.damage = this.START_DAMAGE; }
            else if (numPlayers > 2 && numPlayers < 5) { this.damage = this.START_DAMAGE + 6; }
            else { this.damage = this.START_DAMAGE + 10; }
            this.speed = START_SPEED / numPlayers;
            this.value = VALUE;
            this.effect = SpriteEffects.None;
            this.ID = "ninja";
            this.time = 0;
            this.lead = false;
            this.buff = false;
            this.destination = new Vector2(this.position.X, this.position.Y);
            this.start = new Vector2(this.position.X, this.position.Y);
            this.escaped = false;
            this.hitByFlame = false;
            this.ghost = false;
            this.cooldown_ninja = 0;

            // Start with frame 1 of image array
            this.frameCount = 1;
            this.animationCount = 0;
            this.animationMax = 3;

            this.source = new Rectangle(this.frameSkipX * this.frameCount, 0, this.frameSkipX, this.texture.Height);

            rectangle = new Rectangle((int)position.X, (int)position.Y, source.Width, source.Height);

            //get the color data
            colorMap = new Color[source.Width * source.Height];
            this.texture.GetData(0, source, colorMap, 0, source.Width * source.Height);
        }


        public bool doAI(List<Pigs.Weapons.Bullet> bullets, List<pigs.Scenery> obstacles, List<pigs.Pig> pigs)
        {
            if (this.cooldown_ninja > 0)
            {
                this.cooldown_ninja -= 1;
            }
            else
            {
                this.cooldown_ninja = 75;
            }
            // If this pig has recieved the leader buff, triple its current health, double its damage
            // And call makeGhost() on it
            if (this.lead && !this.buff)
            {
                this.health *= 3;
                this.damage *= 2;
                this.makeGhost();
                this.ghost = true;
                this.buff = true;
            }
            // Avoid suicide pigs
            avoidSuicide(pigs, obstacles);
            // If the timer is 0, then a ninja pig can teleport around a bullet
            if (time == 0)
            {
                foreach (Pigs.Weapons.Bullet b in bullets)
                {
                    if (time < 1)
                    {
                        Vector2 center = new Vector2(this.position.X + this.texture.Width / 2, this.position.Y + this.texture.Height / 2);
                        if (Vector2.Distance(center, b.position) < this.texture.Width)
                        {
                            float back;
                            back = b.angle + (float) Math.PI;
                            time = 45 * numPlayers;
                            if (b.position.Y < center.Y && b.position.X < center.X)
                            {
                                this.position = new Vector2(b.position.X + (float)Math.Cos(back) * 100, b.position.Y + (float)Math.Sin(back) * 100);
                            }
                            else
                            {
                                this.position = new Vector2(b.position.X + (float)Math.Cos(back) * 50, b.position.Y + (float)Math.Sin(back) * 50);
                            }
                            return true;
                        }
                    }
                }
            }
            // If it just avoided a bullet, move and decrement its timer
            else if (time > 0)
            {
                time -= 1;
            }
            if (this.ghost && this.speed < this.START_SPEED)
            {
                this.speed += 0.1f;
            }
            this.gohome(obstacles);
            if (health <= 0)
            {
                // By setting the Active flag to false, the game will remove this object from the 
                // active game list
                active = false;
            }
            this.position.X -= this.speed / this.numPlayers;
            return false;
        }

        /// <summary>
        /// Update for ninja pigs
        /// </summary>
        /// <param name="gameTime"></param>
        /// <param name="bullets"></param>
        /// <param name="obstacles"></param>
        /// <param name="pigs"></param>
        public override bool ninjaUpdate(GameTime gameTime, List<Pigs.Weapons.Bullet> bullets, List<pigs.Scenery> obstacles, List<pigs.Pig> pigs)
        {
            if (this.shouldGhost && this.color.A > 70)
            {
                this.color.A--;
                this.color.A--;
                this.color.A--;
            }

            bool tele = false;
            // Standard scenery avoidance
            if(!this.avoidScenery(obstacles))
            {
                tele = this.doAI(bullets, obstacles, pigs);
            }
            rectangle = new Rectangle((int)this.position.X, (int)this.position.Y, source.Width, source.Height);
            UpdateAnimation();
            return tele;
        }

        // Perform the animation updates on the image
        public void UpdateAnimation()
        {
            this.animationCount += 1;
            if (this.animationCount > this.animationMax)
            {
                this.animationCount = 0;
                this.frameCount += 1;
                if (this.frameCount == 5)
                {
                    this.frameCount = 0;
                }
            }
        }

        public override void Draw(SpriteBatch spriteBatch)
        {
            // Update the source rectangle, based on where in the animation we are.  
            this.source.X = this.frameSkipX * this.frameCount;

            spriteBatch.Draw(texture, this.position, this.source, color, 0f, Vector2.Zero, 1f, effect, 0f);
        }
    }
}
